#define ADRENALIN_EXPORTS

#include "../include/ShaderHandler.h"

/// Singleton Instance
Adrenalin::ShaderHandler* Adrenalin::Singleton<Adrenalin::ShaderHandler>::_pInstance = NULL;

namespace Adrenalin
{
	// ------------------------------------------------------------------------
	// Constructor
	// ------------------------------------------------------------------------
	ShaderHandler::ShaderHandler()
	{
		_lstVertexShaders.clear();
	}
	// ------------------------------------------------------------------------
	// Function: AddShader
	// Description: Add's a shader to our List if it's not exists
	// ------------------------------------------------------------------------
	EResult ShaderHandler::AddShader(IVertexShader* pVertexShader)
	{
		if(ShaderExists(pVertexShader->GetFilename(), pVertexShader->GetFunction()))
		{
			WRITELOG(L"VertexShader " + pVertexShader->GetFilename() + L" not added, allready exists", LOG_ERROR);
			return EXISTS;
		}
		else
		{
			_lstVertexShaders.push_back(pVertexShader);

			WRITELOG(L"VertexShader " + pVertexShader->GetFilename() + L" added", LOG_INFO);
			return OK;
		}

		return FAIL;
	}
	// ------------------------------------------------------------------------
	// Function: ShaderExists
	// Description: Test if a Shader is allready stored in the list
	// ------------------------------------------------------------------------
	bool ShaderHandler::ShaderExists(Adrenalin::stringw& file, Adrenalin::string& function)
	{
		for( std::list<Adrenalin::IVertexShader*>::iterator i = _lstVertexShaders.begin(); 
			 i != _lstVertexShaders.end();
			 i++ )
		{
			if(((*i)->GetFilename().compare(file) == 0) && ((*i)->GetFunction().compare(function)==0))
			{
				return true;
			}
		}
		return false;
	}
	// ------------------------------------------------------------------------
	// Function: Close
	// Description: Unloads all shaders
	// ------------------------------------------------------------------------
	void ShaderHandler::Close()
	{
		for( std::list<Adrenalin::IVertexShader*>::iterator i = _lstVertexShaders.begin(); 
			 i != _lstVertexShaders.end();
			 i++ )
		{
			(*i)->Close();
		}

		WRITELOG(L"All Shaders unloaded", LOG_INFO);
	}
};